﻿#include "Animation.h"
#include "Game.h"
#include"qdebug.h"
Animation::Animation() :m_speed(500)
{
}
Animation::Animation(int speed)
    :m_speed(speed)
{

}

Animation::Animation(QVector<QPixmap>* const pixmap, int speed)
{
    addFrame(pixmap);
    m_speed = speed;
}



void Animation::addFrame( QVector<QPixmap>* const pixmap)
{
    m_pixmaps = pixmap;
}

void Animation::ModifyFrame(const int index, const QString& filename)
{
    (*m_pixmaps)[index] = QPixmap(filename);
}

void Animation::ModifyFrame(QVector<QPixmap>* const pixmap)
{
    addFrame(pixmap);
}

void Animation::setDelayTime(const int time)
{
    m_speed = time;
}

const QPixmap& Animation::getFrame(int index) const
{
    if (m_pixmaps == nullptr)
    {
        qWarning() << "m_pixmaps为空";
        return QPixmap();
    }
    return (*m_pixmaps)[index];
}

const QPixmap& Animation::getFrame() const
{
    if (m_pixmaps == nullptr)
    {
        qWarning() << "m_pixmaps为空";
        return QPixmap();
    }
    return (*m_pixmaps)[m_index];
}

const qsizetype Animation::count() const
{
    if (m_pixmaps == nullptr)
    {
        qWarning() << "m_pixmaps为空";
        return qsizetype();
    }
    return m_pixmaps->count();
}


const int Animation::getDelayTime() const
{
    return m_speed;
}


void Animation::update()
{
    if(count()>1)
    m_index = globalElapsedTimer.elapsed() / m_speed % count();
    if (m_index == count() - 1)
        m_Once =true;
}

void Animation::draw(QPainter *painter,const QPoint& getPos)
{
    if (m_pixmaps == nullptr)
    {
        qWarning() << "m_pixmaps为空绘制失败";
        return;
    }
    painter->drawPixmap(getPos,(*m_pixmaps)[m_index]);
}
